package org.six11.olive3.picturephone.server.handler;

import java.util.Map;

import javax.servlet.ServletException;

import org.six11.olive3.net.Message;
import org.six11.olive3.net.MessageHandler;
import org.six11.olive3.net.MessageNames;
import org.six11.olive3.picturephone.server.PPMessageNames;
import org.six11.olive3.picturephone.server.ServerStateConstants;
import org.six11.olive3.server.DatabaseThing;
import org.six11.olive3.server.SqlStrings;
import org.six11.olive3.util.MultiState;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class RateDoneMessageHandler extends MessageHandler {

  private static double m1 = 0.8;
  private static double m2 = 0.4;

  private DatabaseThing dbThing;
  private MultiState programState;

  public RateDoneMessageHandler(DatabaseThing dbThing, MultiState programState) {
    this.dbThing = dbThing;
    this.programState = programState;
  }

  public void handleLater(Message m) {
    String id_a = m.getParamValue(PPMessageNames.PARAM_SKETCH_ID_A);
    String id_b = m.getParamValue(PPMessageNames.PARAM_SKETCH_ID_B);
    String rate = m.getParamValue(PPMessageNames.PARAM_RATE_VALUE);
    String uid = m.getParamValue(MessageNames.PARAM_USER_ID);

    try {
      // Add the rating
      dbThing.insertRow(SqlStrings.INSERT_JUDGEMENT, id_a, rate, uid);
      bug("User " + uid + " gave sequence ending in sketch " + id_a + " a rating of " + rate);
      // Now give the four users their score:
      // The rater gets a point just for their time,
      // The players who made the younger sketch and the description get X points.
      // The player who made the older sketch gets Y points.
      // X = m1 * rating^2 - 1.5
      // Y = m1 * rating^2 - 1.5
      // where m1 and m2 are params I can play with. (static values above)
      double rating = Double.parseDouble(rate);
      Map<String, String> row;
      row = dbThing.getRow(SqlStrings.FIND_SKETCH_USER, id_a);
      String yougerID = row.get("user_id");
      row = dbThing.getRow(SqlStrings.FIND_SKETCH_PARENT_USER, id_a);
      String describerID = row.get("user_id");
      row = dbThing.getRow(SqlStrings.FIND_SKETCH_USER, id_b);
      String olderID = row.get("user_id");
      int raterScore = 1;
      int youngerScore = (int) ((m1 * Math.pow(rating, 2.0)) - 1.5);
      int olderScore = (int) ((m2 * Math.pow(rating, 2.0)) - 1.5);
      ScoreHandler scoreHandler = (ScoreHandler) programState
          .getValue(ServerStateConstants.HANDLER_SCORE);
      scoreHandler.addScore(uid, "" + raterScore, "+" + raterScore + " points for adding a rating");
      scoreHandler.addScore(yougerID, "" + youngerScore, "+" + youngerScore
          + " points for your sketch");
      scoreHandler.addScore(describerID, "" + youngerScore, "+" + youngerScore
          + " points for your description");
      scoreHandler.addScore(olderID, "" + olderScore, "+" + youngerScore
          + " points for your sketch");
    } catch (ServletException ex) {
      ex.printStackTrace();
    }
  }

}
